package com.zxh.mall.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.Annotation;
import java.util.HashSet;

/**
 * @author zhengxh
 * @version 1.0
 * @date 2024/4/12 - 04 - 12 - 19:16
 * @description com.zxh.mall.common.valid
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private HashSet<Integer> set = new HashSet<>();
    /**
     * 初始化的方法
     * 举例：@ListValue(val={1,0})
     * 获取到1和0
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] val = constraintAnnotation.val();// 0 1
        for (int i : val) {
            set.add(i);
        }
    }

    /**
     * 判断校验是否成功的方法
     * @param integer 客户端传递的对应的属性的值 判断value是否在0，1中
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
